home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_mac / e_access.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-13  |  1.8 KB  |  88 lines  |  [TEXT/CWIE]

  1. /* access.c */
  2.  
  3. #include <Files.h>
  4. #include <Aliases.h>
  5. #include <errno.h>
  6. #include "sys/unistd.h"
  7.  
  8. #if defined(__MWERKS__) && (__MWERKS__ < 700)
  9. #define ioACUser filler2
  10. #endif
  11.  
  12. int access( char *filename, int flags )
  13. {
  14.   FSSpec fss;
  15.   OSErr err;
  16.   CInfoPBRec pb;
  17.   Boolean isFolder, wasAliased;
  18.   Str255 buf;
  19.   long cur_dir;
  20.   short cur_vol;
  21.  
  22.   c_to_p( filename, buf );
  23.   HGetVol( NULL, &cur_vol, &cur_dir );
  24.   // added to deal with aliases...
  25.   err = FSMakeFSSpec( cur_vol, cur_dir, buf, &fss );
  26.   if( err != noErr || ResolveAliasFile( &fss, 1, &isFolder, &wasAliased ) != noErr)
  27.     return -1;
  28.   pb.hFileInfo.ioCompletion = nil;
  29.   pb.hFileInfo.ioVRefNum = fss.vRefNum; // was: cur_vol
  30.   pb.hFileInfo.ioDirID = fss.parID;     // was: cur_dir
  31.   pb.hFileInfo.ioFDirIndex = 0;
  32.   pb.hFileInfo.ioNamePtr = fss.name;
  33.   if( PBGetCatInfoSync(&pb) != 0 ) { errno = ENOENT; return -1; }
  34.   if( pb.dirInfo.ioFlAttrib & 0x10 )
  35.   { // directory
  36.         if( flags & W_OK != 0 )
  37.       {
  38.         if( pb.dirInfo.ioACUser & 0x04 ) /* locked */
  39.         { 
  40.           errno = EIO;
  41.           return -1;
  42.         }
  43.       }
  44.       if( flags & X_OK != 0 )
  45.       {
  46.         if( pb.dirInfo.ioACUser & 0x01 ) /* no "see folders" priv */
  47.         { 
  48.           errno = EIO;
  49.           return -1;
  50.         }
  51.       }
  52.       if( flags & R_OK != 0 ) { }
  53.       {
  54.         if( pb.dirInfo.ioACUser & 0x02 ) /* no "see files" priv */
  55.         { 
  56.           errno = EIO;
  57.           return -1;
  58.         }
  59.       }
  60.   }
  61.   else
  62.   { // file
  63.         if( flags & W_OK != 0 )
  64.       {
  65.         if( pb.hFileInfo.ioFlAttrib & 0x01 ) /* locked */
  66.         { 
  67.           errno = EIO;
  68.           return -1;
  69.         }
  70.       }
  71.       if( flags & X_OK != 0 )
  72.       {
  73.         switch (pb.hFileInfo.ioFlFndrInfo.fdType)
  74.         { case 'APPL':
  75.           case 'BINA':
  76.             break;
  77.           default:
  78.             errno = EIO;
  79.             return -1;
  80.         }
  81.       }
  82.       // if( flags & R_OK != 0 ) { }
  83.   }
  84.   return 0; /* All is well */
  85. }
  86.  
  87. // end
  88.